#include "GameStateMgr.h"

// START: SINGLETON SETUP
GameStateMgr* GameStateMgr::stateMgr = NULL;

GameStateMgr* GameStateMgr::getSingleton(void)
{
	if(stateMgr == NULL)
		stateMgr = new GameStateMgr();

	return stateMgr;
}
// END: SINGLETON SETUP


GameStateMgr::GameStateMgr()
{
}

GameStateMgr::~GameStateMgr()
{
	// Cleanup
}

void GameStateMgr::start(void)
{
	// Call updates on the states.
}

void GameStateMgr::changeState(GameState* state)
{
	// States at the end of the vector list is always on focus, so simply changing that state
	// will change to a new state
	// Cleanup the current state running
	if(!m_states.empty())
	{
		m_states.back()->exit(); // Exit function call
		m_states.pop_back(); // Remove last state
	}

	// Store  and init the new state
	m_states.push_back(state);
	m_states.back()->enter();	// Begin the new state
}

void GameStateMgr::pushState(GameState* state)
{
	// Push state goes into a new state leaving previous state on "Pause"
	if(!m_states.empty())
	{
		m_states.back()->pause();
	}

	m_states.push_back(state);
	m_states.back()->enter();

}

void GameStateMgr::popState()
{
	// Used in conjunction with pushState function. Call this function to return to previous
	// state on "Pause"
	// NOTE: current manager must require you to call this state if another state is on "pause"
	//		 eventually this will be made automatic upon exiting a pushed state.

	if(!m_states.empty())
	{
		m_states.back()->exit();
		m_states.pop_back();
	}


	if(!m_states.empty())
	{
		m_states.back()->resume();
	}
}